package com.ihcil.forecast.demo.listenser;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ihcil.forecast.demo.mapper.RadarMapper;
import com.ihcil.forecast.demo.pojo.Radar;
import com.ihcil.forecast.demo.pyscripts.PyScripts;
import com.ihcil.forecast.demo.utils.Str2Time;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.nio.file.Files;
import java.time.LocalDateTime;

/**
 * Create by @baseException on 2022/11/3 21:45
 */

@Slf4j
@Component
public class FileListener extends FileAlterationListenerAdaptor {

    @Autowired
    private RadarMapper radarMapper;
    @Autowired
    private PyScripts pyScripts;

    private static String cur;
    private static LocalDateTime cur_time;
    private static String cur6;
    private static String cur12;

    @Override
    public void onStart(FileAlterationObserver observer) {
        super.onStart(observer);
//        System.out.println("onStart");
    }

    @SneakyThrows
    @Override
    public void onDirectoryCreate(File directory) {
        System.out.println("新建：" + directory.getAbsolutePath());
    }

    @Override
    public void onDirectoryChange(File directory) {
        System.out.println("修改：" + directory.getAbsolutePath());
    }

    @Override
    public void onDirectoryDelete(File directory) {
        System.out.println("删除：" + directory.getAbsolutePath());
    }

    @SneakyThrows
    @Override
    public void onFileCreate(File file) {
        String compressedPath = file.getAbsolutePath();
        System.out.println("新建：" + compressedPath);
        if (compressedPath.contains("datas")) {

            log.info("监测到新的原始雷达数据!!!");
            pyScripts.runPython();
        }else if (compressedPath.contains("data0")) {

            log.info("对原始雷达数据进行处理...");
            cur = Str2Time.getImgPath(compressedPath);
            cur_time = Str2Time.getPathTime(compressedPath);
        }else if (compressedPath.contains("data6")) {

            log.info("6min后的雷达预测图像保存中....");
            cur6 = Str2Time.getImgPath(compressedPath);
        }else if (compressedPath.contains("data12")) {

            log.info("12min后的雷达预测图像保存中...");
            cur12 = Str2Time.getImgPath(compressedPath);

            Radar radar = new Radar();
            File[] files = new File("E:\\ifstartjustcarryon\\RadarPred\\data12\\").listFiles();

            radar.setId(files.length);
            radar.setTime(cur_time);
            radar.setName("wmj");
            radar.setCur(cur);
            radar.setCur6(cur6);
            radar.setCur12(cur12);
            radarMapper.insert(radar);


//            synchronized (this) {
//                Radar radar = new Radar();
//                File[] files = new File("C:\\Forecast\\data0\\").listFiles();
//                radar.setId(files.length);
//                radar.setTime(cur_time);
//                radar.setName("wmj");
//                radar.setCur(cur);
//                radar.setCur6(cur6);
//                radar.setCur12(cur12);
//                radarMapper.insert(radar);
//            }
        }


//        LocalDateTime localDateTime = LocalDateTime.now();

//        Radar radar = new Radar();
////        radar.setId(id);
//        radar.setTime(localDateTime);
//        File img = new File(compressedPath);
//        byte[] bytes = Files.readAllBytes(img.toPath());
////        FileInputStream inputStream = new FileInputStream(img);
////        byte[] bytes = new byte[(int) img.length()];
//        radar.setFile(bytes);
//        radarMapper.insert(radar);
    }

    @Override
    public void onFileChange(File file) {
        String compressedPath = file.getAbsolutePath();
        System.out.println("修改：" + compressedPath);
    }

    @Override
    public void onFileDelete(File file) {
        System.out.println("删除：" + file.getAbsolutePath());
    }

    @Override
    public void onStop(FileAlterationObserver observer) {
        super.onStop(observer);
//        System.out.println("onStop");
    }

//    @Override
//    public void run() {
//
//    }

//    public synchronized void insert() {
//        Radar radar = new Radar();
//        File[] files = new File("C:\\Forecast\\data0\\").listFiles();
//        radar.setId(files.length);
//        radar.setTime(cur_time);
//        radar.setName("wmj");
//        radar.setCur(cur);
//        radar.setCur6(cur6);
//        radar.setCur12(cur12);
//        radarMapper.insert(radar);
//    }

}





